package UIFree
{
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_arrowbutton;
	import UIFree.controls.uif_button;
	import UIFree.controls.uif_label;
	import UIFree.controls.uif_scrollArea;
	import UIFree.controls.uif_vaScrollArea;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class uif_combobox extends uif_label {
		public static const SELECTITEM: String = 'SELECTITEM';
		public static const UNSELECTITEM: String = 'UNSELECTITEM';
		public static const SHOWLIST: String = 'SHOWLIST';
		public static const HIDELIST: String = 'HIDELIST';
		
		private var _list: uif_vaScrollArea;
		private var _button: uif_button;
		private var _selectIndex: int;
		
		public function uif_combobox(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			var inner: Rectangle = getInner();
			_list = createListLayer();
			_list.visible = false;
			_list.addEvent(uif_scrollArea.CREATE_ELEM, doCreateElem);
			_list.area.addEvent(uif_component.RESIZE, doListResize);
			var _bWidth: Number = inner.height * 1.2;
			_button = new uif_arrowbutton(this, inner.x + inner.width - _bWidth,
											inner.y, _bWidth, inner.height, 3, style['button-class']);
			_button.addEventListener(MouseEvent.CLICK, doButtonClick);
			_selectIndex = -1;
		}
		
		protected function createListLayer(): uif_vaScrollArea {
			return new uif_vaScrollArea(this, 0, height, width, 100, style['list-class']);
		}
		
		protected function doButtonClick(e: MouseEvent): void {
			if (_list.visible) doHideList();
			else {
				doShowList();
				e.stopPropagation();			
			}
		}
		
		protected function doListResize(e: Event): void {
			var l_height: int = (style['list-height'] == 'auto')?_list.area.height:
								((style['list-height']>_list.area.height)?_list.area.height:style['list-height']);
			_list.height = l_height + getSpace() * 2;
		}
		
		protected function doShowList(): void {
			_list.slideShow();
			function doAppDown(e: MouseEvent): void {
				doHideList();
				app.removeEventListener(MouseEvent.CLICK, doAppDown);
			}
			app.addEventListener(MouseEvent.CLICK, doAppDown);
			dispatchEvent(new ObjectEvent(SHOWLIST, this));
		}
		
		protected function doHideList(): void {
			_list.slideHide();
			dispatchEvent(new ObjectEvent(HIDELIST, this));
		}
		
		public function get selectIndex(): int {
			return _selectIndex;
		}
		
		public function itemControl(index: int): * {
			return this.listControl.area.getChildAt(index);
		}
		
		public function set selectIndex(a_selectIndex: int): void {
			if (_selectIndex > -1) {
				this.dispatchEvent(new ObjectEvent(UNSELECTITEM, _selectIndex));
				itemControl(_selectIndex).styleClass = style['item-class'];
			}
			_selectIndex = a_selectIndex;
			var ic: uif_label = itemControl(_selectIndex) as uif_label;
			ic.styleClass = style['select-item-class'];
			if (ic) text = ic.text;			
			this.dispatchEvent(new ObjectEvent(SELECTITEM, _selectIndex));
		}
		
		protected function doSelect(e: MouseEvent): void {
			selectIndex = _list.area.getChildIndex(e.target as DisplayObject);
		}
		
		protected function doCreateElem(e: ObjectEvent): void {
			(e.object as uif_component).addEventListener(MouseEvent.CLICK, doSelect);
		}
		
		public function get listControl(): uif_vaScrollArea {
			return _list;
		}
	}
}